<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'set';
if($_RQ['op']=='setnext'){
	$_RQ['op']='set'.$_SESSION['setjuese'];
}
if($_RQ['op']=='set5' || $_RQ['op']=='set6' || $_RQ['op']=='set7' || $_RQ['op']=='set8' || $_RQ['op']=='set9' || $_RQ['op']=='set10'){
	$_SESSION['setjuese']=str_replace('set','',$_RQ['op']);
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->model('uplog')->addgenjin_log($post);
		$update=array('up_time'=>time());
		if($_RQ['op']=='set9'){
			if($post['status']==1){
				$status=10;
			}elseif($post['status']==0){
				$status=9;
			}elseif($post['status']==2){
				$status=9;
			}
		}elseif($_RQ['op']=='set5'){
			if($post['status']==1){
				$status=2;
			}elseif($post['status']==0){
				$status=31;
			}elseif($post['status']==2){
				$status=30;
			}elseif($post['status']==4){
				$status=110;
			}
			if($post['huifang_time'] && (!$post['status'] || $post['status']==2)){
				$update['huifang_time']=strtotime($post['huifang_time']);
			}
		}elseif($_RQ['op']=='set7'){
			if($post['status']<=1){
				exi('请选择【跟进状态】','error');
			}
			if($post['status']==9){
				$status=6;
			}elseif($post['status']==5){
				$status=32;
				$update['t5liu_time']=time();
			}elseif($post['status']==7){
				$status=32;
				$update['t5wu_time']=time();
			}elseif($post['status']==6){
				$status=32;
				$update['ti5_time']=0;
			}elseif($post['status']==10){
				$status=32;
				$update['ti6_time']=0;
				$update['t6liu_time']=time();
			}elseif($post['status']==8){
				$status=40;
				$update['ti5_time']=time();
			}elseif($post['status']==11){
				$status=42;
				$update['ti6_time']=time();
			}elseif($post['status']==12){
				$status=34;
			}
		}elseif($_RQ['op']=='set8'){
			if($post['status']<=1){
				exi('请选择【跟进状态】','error');
			}
			if($post['status']==13 || $post['status']==16){
				$status=32;
				$update['t7fail_time']=time();
			}elseif($post['status']==18){
				$status=32;
			}elseif($post['status']==17){
				$status=32;
				$update['t7liu_time']=time();
			}elseif($post['status']==14){
				$status=41;
				$update['ti7_time']=time();
			}elseif($post['status']==15 || $post['type']==11){
				$update['t7fail_time']=0;
				$update['t7liu_time']=0;
				$status=8;
			}
		}elseif($_RQ['op']=='set9'){
			if($post['status']==1){
				$status=2;
			}elseif($post['status']==0){
					$status=31;
			}elseif($post['status']==2){
					$status=30;
			}elseif($post['status']==4){
					$status=110;
			}
		}elseif($_RQ['op']=='set6' || $_RQ['op']=='set10'){
			if($post['status']==1){
					$status=32;
			}elseif($post['status']==0){
					$status=32;
			}elseif($post['status']==2){
					$status=32;
			}elseif($post['status']==4){
					$status=100;
			}
			if($post['huifang_time'] && (!$post['status'] || $post['status']==2)){
				$update['huifang_time']=strtotime($post['huifang_time']);
			}
		}
		if($post['status']==1 && $this->genjin_totype[$post['type']]){
			$status=$this->genjin_totype[$post['type']];
		}
		if($_RQ['op']=='set5' && $status==3 && $post['tostoreid'] && $post['tostoreid']!=$post['storeid']){
			$update['fstoreid']=$post['storeid'];
			$update['storeid']=$post['tostoreid'];	
			$update['worker4id']=0;
			$update['worker3id']=0;
		}
		$memberstatus=pdo_getcolumn('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['memberid']),'status');
		if($memberstatus!=33 && $memberstatus!=34 && $status){
			$update['status']=$status;
		}
		if($_SESSION['setjuese']>=5 && $_SESSION['setjuese']<=10){
			$nextid=pdo_getcolumn('jy_yxw_member',array('id <'=>$post['memberid'],'worker'.$_SESSION['setjuese'].'id'=>$_SESSION['workerid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'status'=>$memberstatus),'id');
			if(!$nextid){
				$nextid=pdo_getcolumn('jy_yxw_member',array('id <>'=>$post['memberid'],'worker'.$_SESSION['setjuese'].'id'=>$_SESSION['workerid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'status'=>$memberstatus),'id');
			}
		}
		if($update['status']==-1){
			$this->model('uplog')->upworkerlogstatus($post['memberid'],-1);
		}
		$this->model('uplog')->upworkerlog2($_RQ['op'],$post['memberid'],$post['type'],$post['status']);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['memberid']));
		if($nextid){
			exi('提交成功！','','jy_yxw/xiaoshou.setgenjin_log/setnext/id='.$nextid);
		}else{
			if($_RQ['op']=='set5'){
				$backlist='/juese5.list//status='.$memberstatus;
			}elseif($_RQ['op']=='set6'){
				$backlist='/juese6.list//status='.$memberstatus;
			}elseif($_RQ['op']=='set7'){
				$backlist='/juese7.list//status='.$memberstatus;
			}elseif($_RQ['op']=='set8'){
				$backlist='/juese8.list//status='.$memberstatus;
			}elseif($_RQ['op']=='set9'){
				$backlist='/juese8.list//status='.$memberstatus;
			}elseif($_RQ['op']=='set10'){
				$backlist='/shouhou.koubei/';
			}
			exi('提交成功！','',$backlist);
		}
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['statustitle']=$this->member_status[$item['status']];
		$item['memberid']=$_RQ['id'];
		$item['member']=$this->model('member')->member_show($item['memberid']);
		unset($item['id']);
		if($_SESSION['setjuese']==5){
			$status=array(1,2,30,31);
		}elseif($_SESSION['setjuese']==6){
			$status=array(2,4,32,6,8);
		}elseif($_SESSION['setjuese']==7){
			$status=array(5,6,40,42);
		}elseif($_SESSION['setjuese']==8){
			$status=array(7,41);
		}elseif($_SESSION['setjuese']==9){
			$status=array(7,41);
		}
		$item['mstatus']=$item['status'];
		$status=$item['status'];
		if($_RQ['op']!='set10'){
			$nextid=pdo_getcolumn('jy_yxw_member',array('id <'=>$_RQ['id'],'worker'.$_SESSION['setjuese'].'id'=>array($_SESSION['workerid']),'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status),'id');
			if(!$nextid){
				$nextid=pdo_getcolumn('jy_yxw_member',array('id <>'=>$_RQ['id'],'worker'.$_SESSION['setjuese'].'id'=>array($_SESSION['workerid']),'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status),'id');
			}
			$return['nextid']=$nextid;
		}
		$return['juese_id']=$_SESSION['setjuese'];
	}else{
		exi('请先选择客户','error');
	}
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
	$item['status']=1;
	$item['huifang_time']=$item['huifang_time']?date('Y-m-d H:i',$item['huifang_time']):date('Y-m-d H:i');
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'跟进信息',
	);
	$htmls['edit']=array(
		array('name'=>'member','title'=>'客户信息','type'=>'member'),
		array('type'=>'copy','title'=>'客户电话','name'=>'mobile'),
		array('type'=>'text','title'=>'跟进信息','name'=>'remark'),
		array('type'=>'radio','title'=>'跟进状态','name'=>'status','options'=>'status'),
		array('type'=>'radio','title'=>'跟进效果','name'=>'type','options'=>'genjin_type','show'=>array('status'=>array(1))),
		array('type'=>'time','title'=>'回访时间','name'=>'huifang_time','show'=>array('status'=>array(0,2))),
	);
	$return['status']=$this->genjin_status1;
	if($_RQ['op']=='set9'){
		$return['genjin_type']=$this->genjin_type5;
		unset($htmls['edit'][3]);
	}elseif($_RQ['op']=='set5'){
		$return['genjin_type']=$this->genjin_type1;
		$htmls['edit'][]=array('type'=>'select','title'=>'转到其他门店','name'=>'tostoreid','options'=>'stores','show'=>array('type'=>array(3)));
		$return['stores']=$this->getall('store',array('type'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	}elseif($_RQ['op']=='set7'){
		$return['genjin_type']=$this->genjin_type3;
		$return['status']=$this->genjin_status7;
	}elseif($_RQ['op']=='set8'){
		//$return['genjin_type']=$this->genjin_type4;
		$return['status']=$this->genjin_status8;
	}else{
		if($item['from_type']==2){
			$return['genjin_type']=$this->genjin_type0;
		}else{
			$return['genjin_type']=$this->genjin_type2;
		}
	}
	//$return['htmls']=$htmls;
	$return['sex']=$this->sex;
	$return['xueli_type']=$this->xueli_type;
	$return['from_type']=$this->member_from_type;
	$return['member_from1_type']=$this->member_from1_type;
	$return['member_from2_type']=$this->member_from2_type;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['memberid']=intval($item['memberid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$list=pdo_getslice('jy_yxw_genjin_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['workername']=pdo_getcolumn('jy_yxw_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['workername']=$item['workername']?:'无';
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='setmember'){
	$postdata=get_postdata();
	$postdata['id']=$postdata['memberid'];
	$this->model('member')->add($postdata);
	exi('保存成功！');
}